Find The Active Window and Form in VB.NET

For a particular assignment I had at GE, it was required to determine if a particular form was already open and if so prevent that form from being opened again. The funny thing was I ended up not using it because I went with a simpler solution of using a boolean that was assigned as true if the form was opened then to false when closed. But I spent time on it so hopefully it will help you too. It detects the name of the form, the name of the app, the name of the process id.


Join me on Facebook

Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
    Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessID As Integer) As Integer
    Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As IntPtr, ByVal WinTitle As String, ByVal MaxLength As Integer) As Integer
    Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Integer

    Private Sub timerActiveWindowCheck_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerActiveWindowCheck.Tick

        ‘—– Get the Handle to the Current Forground Window —–

        Dim hWnd As IntPtr = GetForegroundWindow()

        If hWnd = IntPtr.Zero Then Exit Sub

        ‘—– Find the Length of the Window’s Title —–

        Dim TitleLength As Integer

        TitleLength = GetWindowTextLength(hWnd)

        ‘—– Find the Window’s Title —–

        Dim WindowTitle As String = StrDup(TitleLength + 1, "*")

        GetWindowText(hWnd, WindowTitle, TitleLength + 1)

        ‘—– Find the PID of the Application that Owns the Window —–

        Dim pid As Integer = 0

        GetWindowThreadProcessId(hWnd, pid)

        If pid = 0 Then Exit Sub

        ‘—– Get the actual PROCESS from the process ID —–

        Dim proc As Process = Process.GetProcessById(pid)

        If proc Is Nothing Then Exit Sub

        txtProcessID = pid.ToString

        txtProcessName = proc.ProcessName

        txtProcessTitle = proc.MainWindowTitle

        txtCurrentWindowTitle = WindowTitle

        txtTitleLength = TitleLength.ToString

    End Sub

Technorati Tags: ,,,,,,,,,,,,,,,,,,,

Windows Live Tags: vb.net,csharp,.NET Framework,Window,Find,Active,Form,Alias,WinTitle,System,Tick,Title,TitleLength,WindowTitle,Application,GetProcessById,ProcessName,MainWindowTitle,hwnd,proc

  1. #1 by David on November 22, 2009 - 7:53 pm

    You are a champion. I have searched for a very long time for a code that does this that will work in visual studio 2008, and yours is the first that doesnt require extreme rewriting. Thankyou heaps

  2. #2 by Yen on February 23, 2010 - 4:05 am

    Awesome work Kelly. You\’ve just saved me a heap of time. I reckon this will be very handy for me.cheers

  3. #3 by Kelly's Chronicles on August 13, 2011 - 11:46 pm

    Imports System.Data
    Imports System.Data.OleDb
    Public Class Form1
    Private Declare Function GetForegroundWindow Lib “user32.dll” () As IntPtr
    Private Declare Function GetWindowThreadProcessId Lib “user32.dll” (ByVal hwnd As IntPtr, ByRef lpdwProcessID As Integer) As Integer
    Private Declare Function GetWindowText Lib “user32.dll” Alias “GetWindowTextA” (ByVal hWnd As IntPtr, ByVal WinTitle As String, ByVal MaxLength As Integer) As Integer
    Private Declare Function GetWindowTextLength Lib “user32.dll” Alias “GetWindowTextLengthA” (ByVal hwnd As Long) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ‘—– Get the Handle to the Current Forground Window —–
    Dim hWnd As IntPtr = GetForegroundWindow()
    If hWnd = IntPtr.Zero Then Exit Sub

    ‘—– Find the Length of the Window’s Title —–
    Dim TitleLength As Integer
    TitleLength = GetWindowTextLength(hWnd)

    ‘—– Find the Window’s Title —–
    Dim WindowTitle As String = StrDup(TitleLength + 1, “*”)
    GetWindowText(hWnd, WindowTitle, TitleLength + 1)

    ‘—– Find the PID of the Application that Owns the Window —–
    Dim pid As Integer = 0
    GetWindowThreadProcessId(hWnd, pid)
    If pid = 0 Then Exit Sub

    ‘—– Get the actual PROCESS from the process ID —–
    Dim proc As Process = Process.GetProcessById(pid)
    If proc Is Nothing Then Exit Sub
    TextBox1.Text = pid.ToString
    TextBox2.Text = proc.ProcessName
    TextBox3.Text = proc.MainWindowTitle
    TextBox4.Text = WindowTitle
    TextBox5.Text = TitleLength.ToString
    End Sub
    End Class

  4. #4 by BGM on October 27, 2011 - 11:59 pm

    @Kelly, you are giving up a pinvoke error on GetWindowTextLength.
    Your declaration should be:

    Private Declare Function GetWindowTextLength Lib “user32.dll” Alias “GetWindowTextLengthA” (ByVal hwnd As IntPtr) As Integer

    – that is hwnd as IntPtr —–instead of hwnd as Long

  5. #5 by Sumo on May 24, 2012 - 6:45 am

    I need to develop a application to perform leftclick on the window whenever a particular HoptKey is used…But the issue is i couldnt perform the action on the Game ….. ( I could on the windows application form alone….)Pls help me …

  6. #6 by prabaharan on June 10, 2012 - 2:50 am

    thank u mr Kelly’s Chronicles…,
    the code is working fine…,

  7. #7 by Julian on September 5, 2017 - 10:17 am

    @Sumo
    You are right, thank You.

Leave a comment